package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectPaper;
import com.EM1.www.dao.ConnectQuestion;
import com.EM1.www.po.Paper;
import com.EM1.www.po.Question;
import com.EM1.www.util.UpdateSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/DeletePaperQuestion")
public class DeletePaperQuestion extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        int questionid=Integer.parseInt(id);
        HttpSession session = request.getSession();
        Paper paper = (Paper) session.getAttribute("paper");
        ConnectPaper connectPaper=new ConnectPaper();
        try {
            connectPaper.DeletePaperQuestion(questionid,paper.getId());
            UpdateSession.updatePaper(request);
            Paper newpaper = connectPaper.selectnewPaper(paper);
            session.setAttribute("paper",newpaper);
            ConnectQuestion connectQuestion=new ConnectQuestion();
            ArrayList<Question> questions = connectQuestion.selectQuestion(newpaper.getQuestionId());
            request.setAttribute("questions",questions);
            request.getRequestDispatcher("changepaper.jsp").forward(request,response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
